package net.csdn.business.gateway.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 *  @author: zhangyl
 *  @Date: 2022/06/14 14:17
 *  @Description: 直接实现该接口，对entity中带有fill = FieldFill.INSERT_UPDATE赋值时间
 */
@Component
public class DateMetaObjectHandler implements MetaObjectHandler {

    private final static String CREATED_DATE = "createdDate";
    private final static String CREATED_BY = "createdBy";
    private final static String LAST_MOD_DATE = "lastModifiedDate";
    private final static String LAST_MOD_BY = "lastModifiedBy";


    @Override
    public void insertFill(MetaObject metaObject) {
        Object createdDate = getFieldValByName(CREATED_DATE, metaObject);
        Object lastModifiedDate = getFieldValByName(LAST_MOD_BY, metaObject);
        if (createdDate == null || lastModifiedDate == null) {
            Date date = new Date();
            if (createdDate == null) {
                setFieldValByName(CREATED_DATE, date, metaObject);
            }
            if (lastModifiedDate == null) {
                setFieldValByName(LAST_MOD_DATE, date, metaObject);
            }
        }

       /* Object createdBy = getFieldValByName(CREATED_BY, metaObject);
        Object lastModifiedBy = getFieldValByName(LAST_MOD_BY, metaObject);

        if (createdBy == null) {

            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            // for non servlet env,such as spring test mode
            String userId = null;
            if (attributes != null) {
                HttpServletRequest request = attributes.getRequest();
                userId = request.getHeader(USER_ID_HEADER);
            }
            setFieldValByName(CREATED_BY, isNotEmpty(userId)&&!StringUtils.equals("null",userId) ? Long.valueOf(userId) : -1L, metaObject);

        }

        if (lastModifiedBy == null) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            // for non servlet env,such as spring test mode
            String userId = null;
            if (attributes != null) {
                HttpServletRequest request = attributes.getRequest();
                userId = request.getHeader(USER_ID_HEADER);
            }
            setFieldValByName(LAST_MOD_BY, isNotEmpty(userId)&&!StringUtils.equals("null",userId) ? Long.valueOf(userId) : -1L, metaObject);
        }*/
    }

    /**
     * 更新填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName(LAST_MOD_DATE, new Date(), metaObject);
       /* ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String userId = null;
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            userId = request.getHeader(USER_ID_HEADER);
        }
        setFieldValByName(LAST_MOD_BY, isNotEmpty(userId)&&!StringUtils.equals("null",userId) ? Long.valueOf(userId) : -1L, metaObject);*/
    }
}
